<?php

// ** Documentation
/*______________________________________________________________________________

	Greqo Blogger API Test

	Last Update: Dec 2006
	Author: Tom at klenwell@gmail.com
	Website: http://phosphorusandlime.blogspot.com/

	SUMMARY
		Get a blog's feed 
	
	NOTES
		one call to greqo_driver.inc.php loads Greqo library
		put login settings in blogger_data.inc.php file
		
		This should be compatible with later versions of PHP 4.
	
	REFERENCES
		http://code.google.com/apis/accounts/AuthForInstalledApps.html#Request
		http://code.google.com/apis/gdata/blogger.html#list_blogs
		
______________________________________________________________________________*/
 
// *** Initialize Session
error_reporting(E_ALL);
session_start();
 
// *** Load Greqo Driver
require_once('../greqo_driver.inc.php');
 
// *** Load Blogger User Login File
require_once('../blogger_data.inc.php');

// *** Script Variables

	// Event Triggers
	$_TRIGGER['get_token'] = 0;
	$_TRIGGER['get_feed'] = 0;
	
	// SESSION Token
	$_SESSION['GDATA']['auth_token'] = ( isset($_SESSION['GDATA']['auth_token']) ) ? $_SESSION['GDATA']['auth_token']: 0;
	
	// HTML
	$_HTML['body'] = '';
	$_HTML['test_bx'] = '<input type="submit" name="get_feed" value="get feed" />';
	$_HTML['form'] = '';
	$_HTML['token'] = "<div style='font-size:8px;'>current blogger token: {$_SESSION['GDATA']['auth_token']}</div>";
	$_HTML['result'] = '<h2>click button to get blog\'s feed</h2>make sure script is properly configured with correct settings';

	
// *** Controller
if ( !empty($_POST['get_feed']) )
{
	$_TRIGGER['get_token'] = 1;		
}


// *** EVENTS

// * Get Token
if ( $_TRIGGER['get_token'] )
{

	$_HTML['test_bx'] = '';
	$_HTML['result'] .= '<p style="font-size:10px; color:#336699;">attempting to retrieve gdata auth token</p>';
 
	if ( greqo_request_clientlogin_token($_GOOGLE['user'], $_GOOGLE['pw'], 'blogger', 1) ) 
	{
		$_HTML['result'] .= <<<HTML
<h4 style="color:green;">token for user {$_BLOGGER['user']} retrieved</h4>
<div style="margin:1em; font-size:9px; color:#999;">token: {$_SESSION['GDATA']['auth_token']}<b></div>
HTML;
		
		$_TRIGGER['get_feed'] = 1;
	}
	else
	{
		trigger_error('unable to retrieve token', E_USER_WARNING);
	}
}

// * Test Post
if ( $_TRIGGER['get_feed'] )
{
	$_HTML['result'] .= '<p style="font-size:10px; color:#336699;">attempting to get feed for blog #'.$GREQO['BLOGGER']['blog_id'].'</p>';
	
	// Get Feed
	if ( $XML_ARRAY = greqo_blogger_fetch_feed($num_entries=30, $start=1, $type='full') )
	{
		$feed_array = print_r($XML_ARRAY, 1);
		$_HTML['result'] .= "<pre>{$feed_array}</pre>";
	}
	else
	{
		$_HTML['result'] .= '<p style="color:red;">request failed</p>';
	}
}


// *** Output

	// form
	$_HTML['form'] = <<<FORM
<h4></h4>
<form name="greqo_auth" method="post" action="{$_SERVER['PHP_SELF']}">
{$_HTML['test_bx']}
</form>
FORM;
		
		$_HTML['body'] = <<<HTML
<h1>Greqo Client User Blog Feed Test</h1>
{$_HTML['result']}
{$_HTML['form']}
<h6><a href="http://code.google.com/p/greqo/">greqo project page</a></h6>

HTML;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Greqo Feed Test</title>
<style type="text/css">body { font-family:arial,sans-serif; color:#999; }</style>
</head>
<body>
<?php echo $_HTML['body'] ?>
</body>
</html>

